{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/llm/gemini.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Gemini"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**NOTE:** Gemini has largely been replaced by Google GenAI. Visit the [Google GenAI page](https://docs.llamaindex.ai/en/stable/examples/llm/google_genai/) for the latest examples and documentation.\n",
    "\n",
    "In this notebook, we show how to use the Gemini text models from Google in LlamaIndex. Check out the [Gemini site](https://ai.google.dev/) or the [announcement](https://deepmind.google/technologies/gemini/).\n",
    "\n",
    "If you're opening this Notebook on colab, you will need to install LlamaIndex 🦙 and the Gemini Python SDK."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install llama-index-llms-gemini llama-index"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Basic Usage\n",
    "\n",
    "You will need to get an API key from [Google AI Studio](https://makersuite.google.com/app/apikey). Once you have one, you can either pass it explicity to the model, or use the `GOOGLE_API_KEY` environment variable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "env: GOOGLE_API_KEY=...\n"
     ]
    }
   ],
   "source": [
    "%env GOOGLE_API_KEY=..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "GOOGLE_API_KEY = \"\"  # add your GOOGLE API key here\n",
    "os.environ[\"GOOGLE_API_KEY\"] = GOOGLE_API_KEY"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.llms.gemini import Gemini\n",
    "\n",
    "llm = Gemini(\n",
    "    model=\"models/gemini-1.5-flash\",\n",
    "    # api_key=\"some key\",  # uses GOOGLE_API_KEY env var by default\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Call `complete` with a prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "In a world of wonder, where dreams take flight,\n",
      "There exists a backpack, a magical sight.\n",
      "Its fabric woven with stardust and grace,\n",
      "Embroidered with spells, an enchanting embrace.\n",
      "\n",
      "With a whisper and a wish, it opens wide,\n",
      "Revealing treasures that shimmer inside.\n",
      "Books that whisper secrets, maps that unfold,\n",
      "A compass that guides, stories yet untold.\n",
      "\n",
      "A pencil that writes poems, a paintbrush that sings,\n",
      "A telescope that captures the stars' gleaming wings.\n",
      "A magnifying glass, revealing nature's art,\n",
      "A kaleidoscope, painting rainbows in your heart.\n",
      "\n",
      "It holds a mirror that reflects your true worth,\n",
      "A locket that keeps memories close to your birth.\n",
      "A journal that captures your hopes and your fears,\n",
      "A flashlight that banishes shadows and clears.\n",
      "\n",
      "With each step you take, the backpack transforms,\n",
      "Adjusting its weight, adapting to storms.\n",
      "It grows or shrinks, as your needs may arise,\n",
      "A faithful companion, beneath sunny skies.\n",
      "\n",
      "When you're lost and alone, it whispers your name,\n",
      "Guiding you back to the path you reclaim.\n",
      "It carries your burdens, lightens your load,\n",
      "A magical backpack, a gift bestowed.\n",
      "\n",
      "So embrace its magic, let your spirit soar,\n",
      "With this wondrous backpack, forever explore.\n",
      "For within its depths, a universe lies,\n",
      "A treasure trove of dreams, beneath vast skies.\n"
     ]
    }
   ],
   "source": [
    "from llama_index.llms.gemini import Gemini\n",
    "\n",
    "resp = llm.complete(\"Write a poem about a magic backpack\")\n",
    "print(resp)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Call `chat` with a list of messages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[parts {\n",
      "  text: \"Hello friend!\"\n",
      "}\n",
      "role: \"user\"\n",
      ", parts {\n",
      "  text: \"Yarr what is shakin\\' matey?\"\n",
      "}\n",
      "role: \"model\"\n",
      ", parts {\n",
      "  text: \"Help me decide what to have for dinner.\"\n",
      "}\n",
      "role: \"user\"\n",
      "]\n",
      "assistant: Ahoy there, matey! Let's set sail on a culinary adventure and find the perfect dinner for ye. Here be some options to consider:\n",
      "\n",
      "1. **Fish and Chips:** Embark on a classic voyage with a hearty portion of golden-fried fish, accompanied by crispy chips. Dip 'em in tartar sauce for a taste that'll make ye shiver me timbers!\n",
      "\n",
      "2. **Lobster Thermidor:** Indulge in a luxurious feast fit for a pirate king. Tender lobster meat, bathed in a creamy, cheesy sauce, will have ye feeling like royalty.\n",
      "\n",
      "3. **Paella:** Set course for the shores of Spain with a vibrant paella. This colorful dish combines rice, seafood, and vegetables in a saffron-infused broth. Ahoy, it's a feast for the eyes and the belly!\n",
      "\n",
      "4. **Surf and Turf:** Experience the best of both worlds with a combination of succulent steak and tender lobster. This hearty meal is sure to satisfy even the hungriest of scallywags.\n",
      "\n",
      "5. **Crab Cakes:** Dive into a platter of golden-brown crab cakes, bursting with fresh crab meat and flavorful seasonings. Served with a tangy remoulade sauce, these treasures will have ye craving more.\n",
      "\n",
      "6. **Oysters Rockefeller:** Embark on a culinary journey to New Orleans with these decadent oysters. Baked with a rich spinach, breadcrumb, and Pernod sauce, they're a taste of the Big Easy that'll leave ye wanting more.\n",
      "\n",
      "7. **Clam Chowder:** Warm yer bones with a hearty bowl of clam chowder. This New England classic, made with fresh clams, potatoes, and a creamy broth, is the perfect antidote to a chilly night.\n",
      "\n",
      "8. **Lobster Rolls:** Set sail for the coast of Maine and indulge in a classic lobster roll. Fresh lobster meat, dressed in a light mayonnaise-based sauce, is nestled in a toasted bun. It's a taste of the sea that'll have ye hooked!\n",
      "\n",
      "9. **Scallops:** Dive into a plate of seared scallops, cooked to perfection and served with a variety of sauces. Whether ye prefer them with a simple lemon butter sauce or a more adventurous mango salsa, these succulent morsels are sure to please.\n",
      "\n",
      "10. **Shrimp Scampi:** Embark on a culinary adventure to Italy with this classic dish. Plump shrimp, sautéed in a garlicky white wine sauce, served over pasta. It's a taste of the Mediterranean that'll transport ye to sunnier shores.\n",
      "\n",
      "No matter what ye choose, matey, make sure it's a feast worthy of a true pirate. Bon appétit!\n"
     ]
    }
   ],
   "source": [
    "from llama_index.core.llms import ChatMessage\n",
    "\n",
    "messages = [\n",
    "    ChatMessage(role=\"user\", content=\"Hello friend!\"),\n",
    "    ChatMessage(role=\"assistant\", content=\"Yarr what is shakin' matey?\"),\n",
    "    ChatMessage(\n",
    "        role=\"user\", content=\"Help me decide what to have for dinner.\"\n",
    "    ),\n",
    "]\n",
    "resp = llm.chat(messages)\n",
    "print(resp)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Streaming"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using `stream_complete` endpoint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "resp = llm.stream_complete(\n",
    "    \"The story of Sourcrust, the bread creature, is really interesting. It all started when...\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "In the heart of a bustling bakery, where the aroma of freshly baked bread filled the air, there lived a peculiar creature named Sourcrust. Sourcrust wasn't like any ordinary loaf of bread; he possessed a unique consciousness and a mischievous personality.\n",
      "\n",
      "It all began when a young baker named Eliza was experimenting with a new sourdough recipe. As she mixed the flour, water, and yeast, she accidentally added a dash of enchanted baking powder. Little did she know that this seemingly insignificant mistake would give birth to a sentient bread creature.\n",
      "\n",
      "As the dough rose and fermented, Sourcrust came to life. He stretched and yawned, his crusty exterior crackling with energy. Eliza was astounded to see her creation moving and speaking. Sourcrust introduced himself with a warm smile and a hearty laugh, his voice resembling the gentle rustling of bread crumbs.\n",
      "\n",
      "Eliza and Sourcrust quickly formed a bond. She taught him how to read and write, and he shared his knowledge of bread-making techniques. Together, they created delicious pastries and loaves that delighted the customers of the bakery.\n",
      "\n",
      "However, Sourcrust's existence was not without its challenges. As a bread creature, he was vulnerable to the elements. He couldn't stay out in the rain or direct sunlight for too long, and he had to be careful not to get burned or squished.\n",
      "\n",
      "Despite these limitations, Sourcrust embraced his unique nature. He found joy in the simple things, like basking in the warmth of the oven or playing hide-and-seek among the flour sacks. He also developed a taste for adventure, often sneaking out of the bakery at night to explore the town.\n",
      "\n",
      "One day, Sourcrust's curiosity led him to the local library, where he discovered a book about magical creatures. He was fascinated by the stories of fairies, elves, and dragons, and he longed to meet one himself.\n",
      "\n",
      "As fate would have it, Sourcrust's wish came true when he encountered a mischievous brownie named Crumbly in the forest. Crumbly was initially wary of Sourcrust, but after learning about his kind nature, he agreed to be his friend.\n",
      "\n",
      "Together, Sourcrust and Crumbly embarked on many thrilling adventures. They battled evil witches, rescued lost children, and even had a tea party with a talking teapot. Their escapades brought joy and laughter to all who crossed their path.\n",
      "\n",
      "As the years passed, Sourcrust became a beloved figure in the town. People would often visit the bakery just to catch a glimpse of the talking bread creature. Eliza was proud of her creation, and she knew that Sourcrust's magic would continue to inspire and entertain generations to come."
     ]
    }
   ],
   "source": [
    "for r in resp:\n",
    "    print(r.text, end=\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using `stream_chat` endpoint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core.llms import ChatMessage\n",
    "\n",
    "messages = [\n",
    "    ChatMessage(role=\"user\", content=\"Hello friend!\"),\n",
    "    ChatMessage(role=\"assistant\", content=\"Yarr what is shakin' matey?\"),\n",
    "    ChatMessage(\n",
    "        role=\"user\", content=\"Help me decide what to have for dinner.\"\n",
    "    ),\n",
    "]\n",
    "resp = llm.stream_chat(messages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ahoy there, matey! Let's set sail on a culinary adventure and find the perfect dinner for ye. Here be some options to consider:\n",
      "\n",
      "1. **Fish and Chips:** Embark on a classic journey with a hearty portion of golden-fried fish, accompanied by crispy chips. Dip 'em in tartar sauce and let the flavors dance on yer tongue.\n",
      "\n",
      "2. **Seafood Paella:** Dive into a vibrant Spanish feast with paella, a delightful mix of rice, seafood treasures like shrimp, mussels, and calamari, all simmering in a flavorful broth.\n",
      "\n",
      "3. **Lobster Roll:** Indulge in a New England delicacy - a succulent lobster roll, where tender lobster meat is nestled in a toasted bun, dressed with butter and a hint of lemon.\n",
      "\n",
      "4. **Grilled Swordfish:** Set your course for a healthy and delicious meal with grilled swordfish. This firm-fleshed fish, seasoned to perfection, will tantalize yer taste buds with its smoky, savory goodness.\n",
      "\n",
      "5. **Crab Cakes:** Embark on a Maryland adventure with crab cakes, a delectable blend of fresh crab meat, breadcrumbs, and seasonings, pan-fried until golden brown. Serve 'em with a tangy remoulade sauce for an extra kick.\n",
      "\n",
      "6. **Shrimp Scampi:** Set sail for Italy with shrimp scampi, a delightful dish featuring succulent shrimp sautéed in a luscious garlic-butter sauce, served over pasta or crusty bread.\n",
      "\n",
      "7. **Clam Chowder:** Dive into a comforting bowl of clam chowder, a New England classic. This creamy soup, brimming with clams, potatoes, and vegetables, will warm yer soul on a chilly night.\n",
      "\n",
      "8. **Oysters Rockefeller:** Indulge in a luxurious treat with oysters Rockefeller, where fresh oysters are baked with a rich, creamy spinach and herb filling, topped with a golden breadcrumb crust.\n",
      "\n",
      "9. **Lobster Thermidor:** Embark on a culinary voyage to France with lobster thermidor, a decadent dish where succulent lobster is bathed in a creamy, flavorful sauce, then baked to perfection.\n",
      "\n",
      "10. **Scallops with Risotto:** Set your course for a sophisticated meal with scallops and risotto. Tender scallops, seared to perfection, are paired with a creamy, flavorful risotto, creating a harmonious balance of flavors.\n",
      "\n",
      "No matter what ye choose, matey, make sure it be a feast fit for a pirate king!"
     ]
    }
   ],
   "source": [
    "for r in resp:\n",
    "    print(r.delta, end=\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using other models\n",
    "\n",
    "The [Gemini model site](https://ai.google.dev/models) lists the models that are currently available, along with their capabilities. You can also use the API to find suitable models."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "models/gemini-pro\n",
      "models/gemini-pro-vision\n",
      "models/gemini-ultra\n"
     ]
    }
   ],
   "source": [
    "import google.generativeai as genai\n",
    "\n",
    "for m in genai.list_models():\n",
    "    if \"generateContent\" in m.supported_generation_methods:\n",
    "        print(m.name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.llms.gemini import Gemini\n",
    "\n",
    "llm = Gemini(model=\"models/gemini-pro\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "In the realm of knowledge, where wisdom resides,\n",
      "A beacon of brilliance, LlamaIndex abides.\n",
      "With a click and a search, a world unfolds,\n",
      "A tapestry of information, stories untold.\n",
      "\n",
      "From the depths of the web, it gathers and gleans,\n",
      "A treasure trove of facts, a vast, vibrant scene.\n",
      "Like a llama in the Andes, graceful and grand,\n",
      "LlamaIndex roams the digital land.\n",
      "\n",
      "Its interface, a symphony of simplicity and grace,\n",
      "Invites the curious to explore this boundless space.\n",
      "With lightning-fast speed, it delivers the truth,\n",
      "A testament to its power, its unwavering ruth.\n",
      "\n",
      "So let us rejoice, in this digital age,\n",
      "For LlamaIndex stands, a beacon, a sage.\n",
      "May its wisdom forever guide our way,\n",
      "As we navigate the vastness of the digital fray.\n"
     ]
    }
   ],
   "source": [
    "resp = llm.complete(\"Write a short, but joyous, ode to LlamaIndex\")\n",
    "print(resp)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Asynchronous API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.llms.gemini import Gemini\n",
    "\n",
    "llm = Gemini()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1. **Wool**: Llamas are known for their soft, luxurious wool, which is highly prized for its warmth, durability, and water-resistant properties. Llama wool is hypoallergenic, making it suitable for individuals with sensitive skin.\n",
      "\n",
      "2. **Pack Animals**: Llamas have been traditionally used as pack animals in the Andes Mountains of South America. They are well-suited for carrying heavy loads over long distances due to their strength, endurance, and ability to navigate challenging terrain.\n",
      "\n",
      "3. **Adaptability**: Llamas are highly adaptable animals that can thrive in various environments, from the high altitudes of the Andes to the deserts of North America. They are known for their ability to withstand extreme temperatures and harsh conditions.\n",
      "\n",
      "4. **Intelligence**: Llamas are intelligent animals that are easy to train and handle. They are known for their calm and gentle nature, making them suitable for various purposes, including trekking, therapy, and companionship.\n",
      "\n",
      "5. **Social Animals**: Llamas are social animals that live in herds. They have a strong sense of community and rely on each other for protection and companionship. Llamas communicate through a variety of vocalizations and body language.\n",
      "\n",
      "6. **Longevity**: Llamas have a relatively long lifespan, with an average life expectancy of 15-20 years. They are known for their hardiness and resilience, making them suitable for long-term companionship and working relationships.\n",
      "\n",
      "7. **Unique Appearance**: Llamas are known for their distinctive appearance, characterized by their long necks, large eyes, and fluffy ears. Their appearance has made them popular in zoos, farms, and as exotic pets.\n",
      "\n",
      "8. **Cultural Significance**: Llamas hold cultural significance in the Andean region, where they have been revered for centuries. They are often associated with strength, endurance, and good fortune. Llamas are featured in traditional Andean art, folklore, and religious ceremonies.\n"
     ]
    }
   ],
   "source": [
    "resp = await llm.acomplete(\"Llamas are famous for \")\n",
    "print(resp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1. **Wool Production:** Llamas are renowned for their luxurious and soft wool, which is highly prized for its warmth, durability, and hypoallergenic properties. Their wool comes in a variety of natural colors, including white, brown, black, and gray, making it a versatile material for textiles and clothing.\n",
      "\n",
      "2. **Pack Animals:** Llamas have been traditionally used as pack animals in the Andes Mountains of South America for centuries. They are well-suited for this role due to their strength, endurance, and ability to navigate difficult terrain. Llamas can carry up to 25% of their body weight, making them valuable for transporting goods and supplies in mountainous regions.\n",
      "\n",
      "3. **Meat and Milk:** Llama meat is a lean and nutritious source of protein, with a flavor similar to venison. It is consumed in many Andean countries and is becoming increasingly popular in other parts of the world. Llamas also produce milk, which is rich in protein and fat and can be used to make cheese, yogurt, and other dairy products.\n",
      "\n",
      "4. **Companionship:** Llamas are intelligent and social animals that can form strong bonds with humans. They are often kept as companion animals due to their gentle nature, curious personalities, and ability to learn tricks. Llamas can provide companionship and entertainment, and they can also be trained to perform various tasks, such as pulling carts or carrying packs.\n",
      "\n",
      "5. **Cultural Significance:** Llamas hold a special place in the cultures of the Andean region. They are considered sacred animals in many indigenous communities and are often featured in traditional ceremonies and festivals. Llamas are also depicted in art, textiles, and other cultural expressions, symbolizing strength, endurance, and connection to the land."
     ]
    }
   ],
   "source": [
    "resp = await llm.astream_complete(\"Llamas are famous for \")\n",
    "async for chunk in resp:\n",
    "    print(chunk.text, end=\"\")"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "name": "gemini.ipynb",
   "toc_visible": true
  },
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
